package io.xxx.omni.oss.repository

import io.xxx.omni.oss.domain.Job
import io.xxx.omni.oss.domain.Checkpoint
import io.xxx.omni.oss.domain.Store
import io.xxx.omni.oss.repository.mapper.CheckpointMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
import java.time.LocalDateTime

@Repository
class CheckpointRepository {

    @Autowired
    private lateinit var checkpointMapper: CheckpointMapper

    fun get(store: Store, job: Job): Checkpoint {
        val now = LocalDateTime.now()
        val jobId = job.id
        var checkpoint = checkpointMapper.selectById(jobId)
        if (checkpoint == null) {    // 任务第一次执行
            checkpoint = Checkpoint(jobId, store.created, now, now)
            checkpointMapper.insert(checkpoint)
        }
        return checkpoint
    }

    fun save(checkpoint: Checkpoint) {
        checkpointMapper.updateById(checkpoint)
    }
}